import requests
import os
import time

def getAllLevelImage(p):
    url = 'https://study.mcfifa.cn/exams/{}.jpg'.format(p)
    response = requests.get(url)
    img_file = response.content
    img_file_path = os.path.abspath(os.path.dirname(os.getcwd())) + '/resource/levelImages/'
    if not os.path.exists(img_file_path):
        os.mkdir(img_file_path)
    with open(img_file_path + 'level_{}.jpg'.format(p), 'wb') as f:
        f.write(img_file)
    time.sleep(1)


def getAllLessonVideo(level, stmp):
    url = 'http://studyadmin.mcfifa.cn/Dances/{}/{}.mp4'.format(level, stmp)
    if level > 10:
        url = 'http://studyadmin.mcfifa.cn/Dances/{}/new/{}.mp4'.format(level, stmp)
    response = requests.get(url)
    if response.status_code != 200:
        return False
    video_file = response.content
    video_file_path = os.path.abspath(os.path.dirname(os.getcwd())) + '/resource/levelVideos/'
    if not os.path.exists(video_file_path):
        os.mkdir(video_file_path)
    with open(video_file_path + 'video_{}_{}.mp4'.format(level, stmp), 'wb') as f:
        f.write(video_file)
        print('video_{}_{}.mp4'.format(level, stmp))
    return True

if __name__ == '__main__':
    # for idx in range(1, 14):
    #     getAllLevelImage(idx)

    find_data = True
    level = 1
    stmp = 1
    while 1:
        while find_data:
            find_data = getAllLessonVideo(level, stmp)
            if find_data:
                stmp += 1
            else:
                stmp = 1
                break
        find_data = True
        level += 1